package com.ssm.collection.core.common.util;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SSMUtils {

	public static boolean isValidString(String str) {
		return (str != null) && (!str.isEmpty());
	}

	public static boolean isAsciiString(String str) {
		if (!isValidString(str)) {
			return false;
		}

		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) >= 128) {
				return false;
			}
		}

		return true;
	}

	public static boolean isCellPhoneNum(String str) {
		if (!isValidString(str)) {
			return false;
		}

		Pattern p = Pattern.compile("^(1)\\d{10}$");
		Matcher m = p.matcher(str);

		return m.matches();
	}

	public static String toString(Object o) {
		if (o == null) {
			return null;
		}

		return o.toString();
	}

	public static String toArrayString(Object[] array, String seperator) {
		if (array == null || array.length == 0) {
			return "";
		}

		StringBuffer sb = new StringBuffer();
		for (Object element : array) {
			sb.append(element.toString()).append(seperator);
		}

		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	public static String subString(String str, int length) {
		if (str.length() <= length)
			return str;

		return str.substring(0, length);
	}

	public static String formateDate(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public static String formateDate(Date date, int addDay, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, addDay);
		return dateFormat.format(calendar.getTime());
	}

	public static String formateDate(long date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public static File AffineTransImage(File file, double d) {
		File tempTargetFile = null;
		try {
			tempTargetFile = File.createTempFile("targetphoto", "." + "jpg");
			AffineTransform transform = new AffineTransform();
			BufferedImage bis = ImageIO.read(file);
			int w = bis.getWidth();
			int h = bis.getHeight();
			int nh = (int) ((w * d * h) / w);
			double sx = (double) w * d / w;
			double sy = (double) nh / h;
			transform.setToScale(sx, sy);
			AffineTransformOp ato = new AffineTransformOp(transform, null);
			BufferedImage bid = new BufferedImage((int) (w * d), nh, BufferedImage.TYPE_3BYTE_BGR);
			ato.filter(bis, bid);
			ImageIO.write(bid, "jpeg", tempTargetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tempTargetFile;
	}

	public static boolean checkPostCode(String postCode) {
		if (postCode.matches("[1-9]\\d{5}(?!\\d)")) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean checkEmail(String email) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	public static String objectConvertJson(Object obj) {
		ObjectMapper mapper = new ObjectMapper();
		// Include.NON_NULL 属性为NULL 不序列化 只对实体有效
		mapper.setSerializationInclusion(Include.NON_NULL);
		try {
			return mapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static Object jsonConvertObject(String jsonStr, Class<?> classType) {
		ObjectMapper objectMapper = new ObjectMapper();
		// 如果有类中不存在的字段 不抛异常
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return objectMapper.readValue(jsonStr, classType);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
